<?php
class SupplierAction extends MemberAction
{
	public function _initialize()
	{
		parent::_initialize();
		$this->dao = D('Supplier');
	}

	public function index()
	{
	    if(D('Member')->where('id = '.$this->memberInfo['id'])->getfield('level')!=0){
	        $this->error('当前角色不能注册商家','http://www.ydzh99.com/143/Index/index.html');
        }
		if ((int)M('Member')->where(array('id' => $this->memberInfo['id']))->getField('is_subscribe') == 0) {
			F('subscribe_returnUrl' . $this->memberInfo['openid'], '刚刚在填写商家信息<a href="http://' . $_SERVER['HTTP_HOST'] . __SELF__ . '">现在回去继续填写。</a>');
		}
		$this->edit();
	}

	//显示修改页
	public function edit()
	{

		$map['member_id'] = array(
				'eq',
				$this->memberInfo['id']
		);
		$this->info = $this->dao->where($map)->find();
		$this->info['area'] = trim($this->info['area'], ',');
		$this->assign('info', $this->info);
		$this->display();

	}

	public function addconnect($mid,$rid){
		D('Member_connect')->data(array('member_id'=>$mid,'referee_id'=>$rid,'type'=>6))->add();
		$sc=D('Member')->where('id = '.$this->memberInfo['id'])->getfield('is_subscribe');
		$data=array('member_id'=>$mid,'smember_id'=>$rid,'level'=>'0','site_id'=>'143','deep'=>'1','is_subscribe'=>$sc,'datetime'=>time(),'oldid'=>'0');
		$a=D('Member_member')->data($data)->add();
	}
	//写入数据
	public function insert($data = '', $jumpUrl = ''){
		if (!isset($_POST)) {
			$this->error('请填写信息再提交');
			exit;
		}
		$_POST['shopname'] = $_POST['susername'];
		$_POST['area'] = ',' . implode(',', $_POST['area']) . ',';
		$_POST['datetime'] = time();
		$baidumap = explode(',', $_POST['baidupoint']);
		$_POST['baidumapX'] = $baidumap[0];
		$_POST['baidumapY'] = $baidumap[1];
		if($this->memberInfo['id']){
			D('Member')->where('id = '.$this->memberInfo['id'])->save(array('site_id' => 143, 'username' => $_POST['login_name'], 'nickname' => $_POST['login_name'], 'password' => md5('123456')));
            D('Member')->addUser($this->memberInfo['id']);
		}
		$_POST['member_id'] = $this->memberInfo['id'];
		$affid = intval($_COOKIE['aff_id']);
		if ($affid > 0) {
			$_POST['aff_id'] = $affid;
		}
		$_POST['site_id'] = __SITEID__;

		//转存经纬度
		$p = curlRequest('http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=' . $baidumap[0] . '&y=' . $baidumap[1]);
		$p = json_decode($p,true);
		$_POST['lx'] = base64_decode($p['x']);
		$_POST['ly'] = base64_decode($p['y']);
		$_POST['geohash'] = encode_geohash($_POST['lx'], $_POST['ly'], 10);
		if ($jumpUrl == '') {
			$jumpUrl = (session('jumpUrl') && session('jumpUrl') != '') ? session('jumpUrl') : U('Admin/' . MODULE_NAME . '/index');
		}
		if(!$this->dao->where('member_id = '.$this->memberInfo['id'])->find()){
            $result = $this->dao->add($_POST);
        }else{
		    $result=$this->dao->where('member_id = '.$this->memberInfo['id'])->getfield('id');
        }
		if (false !== $result) {
			D('Supplier')->create1($result, 1, $this->memberInfo, 143, 0);
			if($_POST['top_id']!=''){
				$this->addconnect($_POST['top_id'],$this->memberInfo['id']);
			}
			$this->success('操作成功');
		} else {
			$this->error($this->dao->getError());
		}
	}

	//这是更新数据
	public function update($data = array(), $jumpUrl = '')
	{
		if (intval($_POST['id']) == 0) {
			$this->insert();
			exit;
		}
		$_POST['shopname'] = $_POST['susername'];
		$_POST['area'] = ',' . implode(',', $_POST['area']) . ',';
		$baidumap = explode(',', $_POST['baidupoint']);
		$_POST['baidumapX'] = $baidumap[0];
		$_POST['baidumapY'] = $baidumap[1];
		//转存经纬度
		$p = curlRequest('http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=' . $baidumap[0] . '&y=' . $baidumap[1]);
		$p = json_decode($p, true);

		$_POST['lx'] = base64_decode($p['x']);
		$_POST['ly'] = base64_decode($p['y']);
		$_POST['geohash'] = encode_geohash($_POST['lx'], $_POST['ly'], 10);

		if ($this->_post('id')) {
			unset($_POST['site_id']);
			$map['id'] = array('eq', $this->_post('id'));
			$result = $this->dao->data($_POST)->save();
			if (false !== $result) {
				$this->success('更新成功');
			} else {
				$this->error($this->dao->getError());
			}
		} else {
			$this->error('参数错误');
		}
	}
}

